/**
 * 
 */
package arisufascion.com.activity;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
import arisufascion.com.AbstractActivity;
import arisufascion.com.R;
import arisufascion.com.constant.Constants;
import arisufascion.com.constant.WriteLog;
import arisufascion.com.handler.ArticleHandler;
import arisufascion.com.handler.ServiceHandler;
import arisufascion.com.model.Article;
import arisufascion.com.network.RequestTask;

/**
 * @author Kelvin
 * 
 */
public class ActivityShipping extends AbstractActivity {
	private TextView txt_title, txt_intro;
	private Article article;
	private boolean isLoading = true;
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_policy);
		txt_title = (TextView) findViewById(R.id.txt_title);
		txt_intro = (TextView) findViewById(R.id.txt_intro);
		findViewById(R.id.btn_privacy).setOnClickListener(onClick);
		findViewById(R.id.btn_wholesale).setOnClickListener(onClick);
		findViewById(R.id.btn_shipping).setOnClickListener(onClick);
		findViewById(R.id.btn_menu).setOnClickListener(onClick);
		
		new getArticlePolicyFromServer().execute();
	}

	public class getArticlePolicyFromServer extends AsyncTask<Void, Void, Void> {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			// Show Dialog
			if (isLoading) {
				dialog = new ProgressDialog(getParent());
				dialog.setMessage("Fetching data...");
				dialog.show();
				isLoading = false;
			}
		}

		@Override
		protected Void doInBackground(Void... params) {

			String url = String.format(Constants.SERVER_URL_ARTICLE,78 ,Constants.SERVER_API_KEY);
			
			
			String json = RequestTask.getJsonFromUrl(url);
			
			WriteLog.d("Kelvin", json);
			
			ServiceHandler service = new ServiceHandler();
			ArticleHandler articleHandler = service.getArticle(json);

			article = articleHandler.getData();

			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			// Dismiss Dialog
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}

			txt_title.setText(article.getTitle());
			txt_intro.setText(Html.fromHtml(article.getIntrotext()));

		}
	}


}
